package love.yuanmengrobot.qdkaoqin.business;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.entity.Clazz;
import love.yuanmengrobot.qdkaoqin.util.Constants;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class JoinClassActivity extends AppCompatActivity {
    public static final String JOINCLASS_MESSAGE="joinClass";
    String memberid=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_joinclass);

        memberid=getIntent().getStringExtra(TeacherConstants.MEMBERID);
        System.out.println(memberid);
        final EditText inClassId=findViewById(R.id.inClassId);

        Button join=findViewById(R.id.join);
        join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                joinClass(memberid,inClassId.getText().toString());
            }
        });
        Button goBack=findViewById(R.id.bu_back);
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void joinClass(final String memberid, final String clazzid){
        new Thread(new Runnable() {
            @Override
            public void run() {
                FormBody.Builder builder = new FormBody.Builder();
                builder.add("memberid", memberid)
                        .add("op","insert")
                        .add("clazzid", clazzid);
                FormBody body = builder.build();
                Request request = new Request.Builder().url(Constants.JOINCLASS_URL).post(body).build();


                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {

                    ex.printStackTrace();
                }
            }
        }).start();
    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println(body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(body.contains("加入成功")){
                            alertDialog("提示","加入成功！");
//                            finish();
//                            startActivity(new Intent(JoinClassActivity.this,StudentHomeActivity.class).putExtra(JOINCLASS_MESSAGE,memberid));
                        }else{
                            alertDialog("提示","无该班级!");
                        }
                    }
                });
            } else {
                alertDialog("错误！","服务请求错误！");
            }
        }
    };

    //定义一个弹窗提示
    public void alertDialog(String title, String content) {
        final AlertDialog alertDialog1 = new AlertDialog.Builder(JoinClassActivity.this)
                .setTitle(title)//标题
                .setMessage(content)//内容
                .setIcon(R.mipmap.ic_warning)
                .create();
        alertDialog1.show();
        final Timer t = new Timer();//设置窗口自动关闭
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                alertDialog1.dismiss();
                t.cancel();
            }
        }, 2000);//2秒后窗口自动关闭
    }
}
